<?php

/**
 *
 *
 * @version $Id$
 * @copyright 2008
 */


/**
 *
 *
 */
class Selector{
   /**
    * Constructor
    * @access protected
    */
    private $select;
    private $from;
    private $critere;
    private $order;
    private $limit;
    private $groupBy;
    
    public static $SENS_TRI_ASC  = 1;
    public static $SENS_TRI_DESC = 0;

   function addChamp($champP){
      if( !empty($this->select) )
      {
         $this->select .= ", ";
      }
      $this->select .= $champP;
   }

   function addFrom($tableP){
      if( !empty($this->from) )
      {
         $this->from .= ", ";
      }
      $this->from .= $tableP;
   }

   function addCritere($champP, $critereP){
      $this->addCritereOperateur('=', $champP, $critereP);
   }

   function addCritereLike($champP, $critereP){
      $this->addCritereOperateur('LIKE', $champP, $critereP);
   }

   function addCritereSpecif($critere){
      if( !empty($this->critere) )
      {
         $this->critere .= " AND ";
      }
      $this->critere .= $critere;
   }

    function addCritereOperateur($operateur, $champP, $critereP){
      if( !empty($this->critere) )
      {
         $this->critere .= " AND ";
      }
      $this->critere .= $champP . " " . $operateur . " '" . $critereP . "'";
   }
   
   function addOrderBy($arrayChamp, $sens)
   {
      foreach($arrayChamp as $cle=>$valeur) 
      {
        // Les Champs
        if( !empty($this->order) )
        {
           $this->order .= ", ";
        } 
        $this->order .= $valeur;
        if( $sens == Selector::$SENS_TRI_ASC)
        {
          $this->order .= ' ASC';
        }
        else
        {
          $this->order .= ' DESC';
        }
      }      
   }

   function addLimit($NumDebut, $nbLimit)
   {
     $this->limit = $NumDebut . ', ' . $nbLimit;
   }
   function addLimite($nbLimit)
   {
      $this->limit = $nbLimit;
   }
   
   function addGroupBy($arrayGroupBy)
   {
      foreach($arrayGroupBy as $cle=>$valeur) 
      {
        // Les Champs
        if( !empty($this->groupBy) )
        {
           $this->groupBy .= ", ";
        } 
         $this->groupBy = $valeur;
      }
    
   }
   /*
   Formate la requete
   */
   function toString() 
   {
      $requete = $this->toStringSsRequete();
      $requete .= ';';
      return $requete;

   }

   function toStringSsRequete()
   {
     $requete = 'SELECT ' . $this->select . ' FROM ' . $this->from;
   
     if ($this->critere != "")
     {
       $requete .= ' WHERE ' . $this->critere;
     }
   
     if ($this->order != "")
     {
       $requete .= ' ORDER BY ' . $this->order;
     }
   
     if ($this->limit != "")
     {
       $requete .= ' LIMIT ' . $this->limit;
     }
   
     if ($this->groupBy != "")
     {
       $requete .= ' GROUP BY ' . $this->groupBy;
     }
   
     return $requete;
   
   }
}

?>